#ifndef _RTC_C_
#define _RTC_C_

#include "rtc.h"

// Timer runs at 4000000
// Low byte divides by 256, therefore High byte counts to 15625/second
// 15625 = 125*125
// Therefore let high byte count to 125 a total of 125 times
// To have high byte count to 125, preload with 256-125 and allow rollover

#define TMR1_SECOND	125
#define TMR1_PRELOAD	256-125

#if FOSC == 4000000
#define T1CON_INIT	0b00000001	// Timer ON, 1:1
#elif FOSC == 16000000
#define T1CON_INIT	0b00100001	// Timer ON, 1:4
#else
#error rtc.c: There is no value for T1CON at the specified clock speed
#endif 

TIME baseTime;
char count;

void rtc_config( void )
{
	TMR1H = 0;
	TMR1L = 0;
	T1CON = T1CON_INIT;
	count = 0;
	resetTime( baseTime );
}


// RTC interrupt handler
void isr_rtc( void )	
{
	TMR1H = TMR1_PRELOAD;

	count++;	
	if( count >= TMR1_SECOND )
	{
		count = 0;
		incTime( baseTime );
	}
}

#endif
